<?php


namespace app\wechat\controller;


use app\BaseController;
use app\common\model\Book;
use app\common\model\Goods;
use app\common\model\UserBook;
use Psr\Cache\CacheException;
use think\cache\driver\Redis;
use think\facade\Cache;
use think\facade\Db;

class Index extends AuthBase
{
//     protected $noLogin = ['index'];
    /**\
     * @return \think\response\Json
     * 首页
     */
    public function index()
    {
//        if (!$this->userId){
//            return show(1,'',1);
//        }
        $type=Db::name('user')
        ->where('id',$this->userId)
        ->value('type');
        return show(1,'',$type);
        if (!$type){
            return show(-1,'',1);
        }
        if ($type==1){
            return show(1,'',$type);
        }
        return show(-1, '', $type);
    }
    /**
     * 个人资产接口
     */
    public function user(){
        $type = input('param.type')??'';
        $where=[];
        if ($type){
            $where[]=[
                'status','=',$type,
            ];
        }
        $user=Db::name('user')
            ->where('id',$this->userId)
            ->field('asset_price,sell_price')
            ->find();
        //总资产
        if (empty($type)){
            $price=$user['asset_price']-$user['sell_price'];
            //总日均//算出所有产品的已使用天数
            $sum_yituiyi=Db::name('goods')
                ->where('user_id',$this->userId)
                ->where('status',2)
                ->sum('serve_day');
            $sum_fuyi=Db::name('goods')
                ->where('user_id',$this->userId)
                ->where('status',1)
                ->column('id,use_date,buy_date');
            $num=0;
            foreach ($sum_fuyi as $k=>$v){
                $num_one=get_serve_time_day($v['buy_date'],time());
                $num+=$num_one;
            }
        }
        if ($type==1){
            $price=Db::name('goods')
                ->where(['user_id'=>$this->userId,'status'=>1])
                ->sum('money');
            $sum_yituiyi=0;
            $sum_fuyi=Db::name('goods')
                ->where('user_id',$this->userId)
                ->where('status',1)
                ->column('id,use_date,buy_date');
            $num=0;
            foreach ($sum_fuyi as $k=>$v){
                $num_one=get_serve_time_day($v['buy_date'],time());
                $num+=$num_one;
            }

        }
        if ($type==2){
            $price=Db::name('goods')
                ->where(['user_id'=>$this->userId,'status'=>2])
                ->sum('money');
            $price_sell=Db::name('goods')
                ->where(['user_id'=>$this->userId,'status'=>2])
                ->sum('sell_price');
            $price-=$price_sell;
            $sum_yituiyi=Db::name('goods')
                ->where('user_id',$this->userId)
                ->where('status',2)
                ->sum('serve_day');
            $num=0;
        }
//        $price=$user['asset_price']-$user['sell_price'];
        //总日均//算出所有产品的已使用天数
//        $sum_yituiyi=Db::name('goods')
//            ->where('user_id',$this->userId)
//            ->sum('serve_day');
//        $sum_fuyi=Db::name('goods')
//            ->where('user_id',$this->userId)
//            ->where('status',1)
//            ->column('id,use_date,buy_date');
//        $num=0;
//        foreach ($sum_fuyi as $k=>$v){
//            $num_one=get_serve_time_day($v['buy_date'],time());
//            $num+=$num_one;
//        }
        //总商品和总服役中
        $sum=Db::name('goods')
            ->where('user_id',$this->userId)
            ->where($where)
            ->count();
//        $fuyi_sum=Db::name('goods')
//            ->where(['user_id'=>$this->userId,'status'=>1])
//            ->count();
        if ($num==0){
            $num=1;
        }
        $rijun=round($price/($num+$sum_yituiyi),2);
        $data['sum_price']=$price;
        $data['rijun']=$rijun;
        $data['num']=$sum;
        return show(1, '', $data);
    }
}